package com.klei.controller;

import com.klei.pojo.CityManager;
import com.klei.service.ICityManagerService;
import com.klei.util.LayuiJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
public class CityManagerController {

    @Autowired
    private ICityManagerService cityManagerService;

    @GetMapping("/getAllManagerCity")
    @ResponseBody
    public LayuiJson getAllManagerCity(){

        LayuiJson layuiJson = new LayuiJson();
        layuiJson.setCode(0);
        layuiJson.setMsg("");
        layuiJson.setData(cityManagerService.getAllCity());

        return layuiJson;
    }

    @GetMapping("/getAllManagerCountry")
    @ResponseBody
    public LayuiJson getAllManagerCountry(){

        LayuiJson layuiJson = new LayuiJson();
        layuiJson.setCode(0);
        layuiJson.setMsg("");
        layuiJson.setData(cityManagerService.getAllCountry());

        return layuiJson;
    }

    @GetMapping("/getAllCityByPage")
    @ResponseBody
    public LayuiJson getAllCityByPage(@RequestParam(name = "page") int page,
                                      @RequestParam(name = "limit") int limit){

        LayuiJson layuiJson = new LayuiJson();
        layuiJson.setCode(0);
        layuiJson.setMsg("");
        layuiJson.setData(cityManagerService.getAllCityByPage((page - 1 ) * limit, limit));
        layuiJson.setCount(cityManagerService.getAllCityCount());

        return layuiJson;
    }

    @GetMapping("/getAllCountryByPage")
    @ResponseBody
    public LayuiJson getAllCountryByPage(@RequestParam(name = "page") int page,
                                         @RequestParam(name = "limit") int limit){

        LayuiJson layuiJson = new LayuiJson();
        layuiJson.setCode(0);
        layuiJson.setMsg("");
        layuiJson.setData(cityManagerService.getAllCountryByPage((page - 1 ) * limit, limit));
        layuiJson.setCount(cityManagerService.getAllCountryCount());

        return layuiJson;
    }

    @GetMapping("/saveCity")
    @ResponseBody
    public boolean saveCity(@RequestParam(name = "pname") String pname){
        System.out.println(pname);
        return cityManagerService.saveCity(pname);
    }

    @PostMapping("/saveCountry")
    @ResponseBody
    public boolean saveCountry(@RequestBody CityManager cityManager){
        return cityManagerService.saveCountry(cityManager);
    }

    @PostMapping("/updateCity")
    @ResponseBody
    public boolean updateCity(@RequestBody CityManager cityManager){
        return cityManagerService.updateCity(cityManager);
    }

    @DeleteMapping("/deleteCityById/{id}")
    @ResponseBody
    public boolean deleteCityById(@PathVariable("id") int id){
        return cityManagerService.deleteCityById(id);
    }

}
